#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

def verifyTAR(filepath):
    import commands

    cmd = "tar -tf \"" + filepath + "\""
    result = commands.getoutput(cmd)
    if (result.find("unexpected end of file") == -1) and (result.find("Unexpected EOF in archive") == -1) and (
                result.find("No such file or directory") == -1) and (
                result.find("This does not look like a tar archive") == -1):
        return True
    else:
        return False


def verifyPDF(filepath):
    infile = open(filepath, "rb")

    l3 = ""
    for line in infile:
        line = line.rstrip()
        line = line.rstrip(chr(00))
        if not line == "":
            l3 = line
    infile.close()
    if l3.find("%%EOF") != -1:
        return True
    else:
        return False


def verifyFile(filepath):
    result = False
    if filepath.endswith(".pdf"):
        result = verifyPDF(filepath)
    elif filepath.endswith(".tar.gz"):
        result = verifyTAR(filepath)
    return result


if __name__ == "__main__":
    import sys

    print verifyFile(sys.argv[1])
